﻿module Tww.Pixxler.ColorFilters

open System
open Tww.Pixxler.ColorTypes
open System.Threading.Tasks


    // Vereinfachungsmethode
    let pfor nfrom nto f = 
        Parallel.For(nfrom, nto + 1, Action<_>(f)) |> ignore

    // BILDFILTER

    // Grayscale
    let Grayscale(clr:SimpleColor) =
        let c = (clr.R*11 + clr.G*59 + clr.B*30) / 100
        SimpleColor(c, c, c)

    // Hellere Farben
    let Lighten(clr:SimpleColor) =
        (clr*2).ClipColor()


    // FUNKTIONEN

    // Wende den übergebenen Filter 'f' auf jedes Element im Array an
    let runFilter f arr = 
        Array2D.map f arr



          // Listing 14.18 Parallel 'map' function for 2D array
    module Array2D =
      module Parallel =
        // Declare function in a module
        // Apply 'f' to all elements in parallel
        let map f (arr:_ [,]) =
          let wid, hgt = arr.GetLength(0), arr.GetLength(1)
          // Create new array as a result
          let res = Array2D.zeroCreate wid hgt
          // Parallelized outer loop
          pfor 0 (wid-1) (fun x ->
            for y = 0 to hgt - 1 do
              res.[x, y] <- f(arr.[x, y]) )
          res
    
    let runFilterParallel f arr  = Array2D.Parallel.map f arr